package com.yu.shortlink.admin.remote.dto;

/**
 * @Author yushiwang
 * @Date 2024/5/21 21:43
 * @PackageName:com.yu.shortlink.admin.remote.dto
 * @ClassName: ShortLinkRemoteService
 * @Version 1.0
 */

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yu.shortlink.admin.common.convention.result.Result;
import com.yu.shortlink.admin.remote.dto.req.*;
import com.yu.shortlink.admin.remote.dto.resp.ShortLinkCreateRespDTO;
import com.yu.shortlink.admin.remote.dto.resp.ShortLinkGroupCountQueryRespDTO;
import com.yu.shortlink.admin.remote.dto.resp.ShortLinkPageRespDTO;
import com.yu.shortlink.admin.remote.dto.resp.ShortLinkStatsAccessRecordRespDTO;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;

/**
 * 短链接中台远程调用服务
 */
public interface ShortLinkRemoteService {
    /**
     * 分页查询短链接
     */
    default Result<IPage<ShortLinkPageRespDTO>> pageShortLink(ShortLinkPageReqDTO requestParam){
        HashMap<String, Object> requestMap = new HashMap<>();
        requestMap.put("gid",requestParam.getGid());
        requestMap.put("current",requestParam.getCurrent());
        requestMap.put("size",requestParam.getSize());
        String resultPageStr = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/page", requestMap);
        return JSON.parseObject(resultPageStr, new TypeReference<>() {
        });
    }

    /**
     * 创建短链接
     */
    default  Result<ShortLinkCreateRespDTO> createShortLink(ShortLinkCreateReqDTO requestParam){
        String resultBodyStr = HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/create", JSON.toJSONString(requestParam));
        return JSON.parseObject(resultBodyStr,new TypeReference<>() {
        });
    }

    /**
     * 查询分组短链接数量
     */
    default Result<List<ShortLinkGroupCountQueryRespDTO>> listGroupShortLinkCount(List<String> requestParam){
        HashMap<String, Object> requestMap = new HashMap<>();
        requestMap.put("requestParam",requestParam);
        String resultPageStr = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/count", requestMap);
        return JSON.parseObject(resultPageStr, new TypeReference<>() {
        });
    }

    default  Result<String> getTitleByUrl(@RequestParam("url")String url){
        String resultPageStr = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/title?url="+url);
        return JSON.parseObject(resultPageStr, new TypeReference<>() {
        });
    }

    default  Result<Void> saveRecycleBin(RecycleBinSaveReqDTO requestParam){
        String resultBodyStr = HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/recycle-bin/save", JSON.toJSONString(requestParam));
        return JSON.parseObject(resultBodyStr,new TypeReference<>() {
        });
    }

    default Result<IPage<ShortLinkPageRespDTO>> pageShortRecycleBinLink(ShortLinkRecycleBinPageReqDTO requestParam){
        HashMap<String, Object> requestMap = new HashMap<>();
        requestMap.put("gidList",requestParam.getGid());
        requestMap.put("current",requestParam.getCurrent());
        requestMap.put("size",requestParam.getSize());
        String resultPageStr = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/recycle-bin/page", requestMap);
        return JSON.parseObject(resultPageStr, new TypeReference<>() {
        });
    }

    default Result<Void> recoverRecycleBin(RecycleBinRecoverReqDTO requestParam){
        String resultBodyStr = HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/recycle-bin/recover", JSON.toJSONString(requestParam));
        return JSON.parseObject(resultBodyStr,new TypeReference<>() {
        });
    };

    default Result<Void> removeRecycleBin(RecycleBinRemoveReqDTO requestParam){
        String resultBodyStr = HttpUtil.post("http://127.0.0.1:8001/api/short-link/v1/recycle-bin/remove", JSON.toJSONString(requestParam));
        return JSON.parseObject(resultBodyStr,new TypeReference<>() {
        });
    };

    /**
     * 监控访问记录数据
     */
    default Result<IPage<ShortLinkStatsAccessRecordRespDTO>> shortLinkAccessRecord(ShortLinkAccessRecordReqDTO requestParam){
        String resultPageStr = HttpUtil.get("http://127.0.0.1:8001/api/short-link/v1/access-record", BeanUtil.beanToMap(requestParam,"orders","records"));
        return JSON.parseObject(resultPageStr, new TypeReference<>() {
        });
    }
}
